package com.macro.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.BmsLeaveApprovalMapper;
import com.macro.mall.mapper.UmsAdminMapper;
import com.macro.mall.mapper.UmsAdminRoleRelationMapper;
import com.macro.mall.model.*;
import com.macro.mall.query.BmsLeaveApprovalQuery;
import com.macro.mall.service.BmsLeaveApprovalService;
import com.macro.mall.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class LeaveApprovalServiceImpl implements BmsLeaveApprovalService {

    @Resource
    BmsLeaveApprovalMapper leaveApprovalMapper;

    @Resource
    UmsAdminRoleRelationMapper adminRoleRelationMapper;

    @Resource
    UmsAdminMapper adminMapper;


    @Override
    public List<BmsLeaveApproval> findList(BmsLeaveApprovalQuery query) {
        Integer pageNum = query.getPageNum();
        Integer pageSize = query.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }

        BmsLeaveApprovalExample example = new BmsLeaveApprovalExample();
        BmsLeaveApprovalExample.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(query.getLeaveType())) {
            criteria.andLeaveTypeEqualTo(Byte.valueOf(query.getLeaveType()));
        }
        return leaveApprovalMapper.selectByExample(example);
    }

    @Override
    public int approval(BmsLeaveApprovalQuery query) {
        String id = query.getId();
        BmsLeaveApproval bmsLeaveApproval = leaveApprovalMapper.selectByPrimaryKey(Long.valueOf(id));

        bmsLeaveApproval.setApprovalUserId(Long.valueOf(query.getApprovalUserId()));
        bmsLeaveApproval.setApprovalUserName(query.getApprovalUserName());

        bmsLeaveApproval.setApprovalTime(new Date());
        bmsLeaveApproval.setUpdateUser(Long.valueOf(query.getApprovalUserId()));
        bmsLeaveApproval.setUpdateTime(new Date());
        bmsLeaveApproval.setApprovalResult(Byte.valueOf("1"));
        bmsLeaveApproval.setApprovalOpinions(query.getApprovalOpinions());

        return leaveApprovalMapper.updateByPrimaryKeySelective(bmsLeaveApproval);
    }

    @Override
    public List<Map<String, String>> getApprovalUserList() {
        UmsAdminRoleRelationExample adminRoleRelationExample = new UmsAdminRoleRelationExample();
        adminRoleRelationExample.createCriteria().andRoleIdEqualTo(Long.valueOf("6"));
        List<UmsAdminRoleRelation> umsAdminRoleRelations = adminRoleRelationMapper.selectByExample(adminRoleRelationExample);
        List<Map<String, String>> userList = new ArrayList<>();
        if (!umsAdminRoleRelations.isEmpty()) {
            umsAdminRoleRelations.forEach( i -> {
                Long adminId = i.getAdminId();
                UmsAdmin umsAdmin = adminMapper.selectByPrimaryKey(adminId);
                Map<String, String> userMap = new HashMap<>();
                userMap.put("id", umsAdmin.getId().toString());
                userMap.put("name", umsAdmin.getUsername());
                userList.add(userMap);
            });
        }

        return userList;
    }
}
